package cc.ttiot.service.dataCollection.serial.protocol.modbusRtu;

import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.serial.SerialPortWrapper;

/**
 * @Description: ModbusRtu的工具类
 * @Author WangSuheng
 * @Date 2021/10/1 15:59
 * @Version 1.0
 */
public class ModbusRtuUtils {
    /* 创建ModbusFactory工厂实例 */
    public static final ModbusFactory modbusFactory = new ModbusFactory();

    //创建RtuMaster实例并初始化
    public static ModbusMaster createAndInit(SerialPortWrapper wrapper) throws ModbusInitException {
        //创建ModbusMaster实例
        ModbusMaster master = modbusFactory.createRtuMaster(wrapper);
        //初始化
        master.init();

        return master;
    }

    //创建ModbusRTUMaster实例
    public static ModbusRtuMaster createModbusRTUMaster(SerialPortWrapper wrapper) throws ModbusInitException {
        return new ModbusRtuMaster(createAndInit(wrapper));
    }
}
